/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.socket.client;

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 *
 * @author CongChinh
 */
public class ClientService {
    private Socket socket;
    private BufferedReader bufferedReader;
    private PrintWriter printWriter;
    private MotorTableModel model;
    private Boolean connected = false;
    public void connect(InetAddress address, int port) throws UnknownHostException, IOException {
            socket = new Socket(address, port);
            printWriter = new PrintWriter(socket.getOutputStream(), true);
            bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));                        
            //send local address to server
            String localAddress = socket.getLocalSocketAddress().toString();            
            send(localAddress);
    }
    public void send(String xmlRequest) {
        printWriter.println(xmlRequest);
    }
    public String receive() throws IOException {
        return bufferedReader.readLine();
    }
    public void disconnect() throws IOException {
        printWriter.close();
        bufferedReader.close();
        socket.close();        
    }
    public Boolean is_connected() {
        return connected;
    }
    public void setConnectStatus(Boolean value) {
        connected = value;
    }
}
